home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2844.asc < prev    next >
Encoding:
Text File  |  1996-09-15  |  11.0 KB  |  305 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2844
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  August 17, 1995                          PAGE  :  1/5
  11.  
  12.     TITLE  :  Using The ASCII Driver With Comma-delimited Files
  13.  
  14.  
  15.  
  16.  
  17. Delphi (and the BDE) has the capability to use ASCII files to a limited
  18. degree as tables. The ASCII driver has the capability to translate the
  19. data values in an ASCII fixed-length field or a comma-delimted file into
  20. fields and values that can be displayed through a TTable component. How
  21. this translation of the ASCII file takes place depends on an accompanying
  22. schema file. The schema file for an ASCII data file defines various attri-
  23. butes necessary for parsing the ASCII data file into individual field
  24. values. The field definitions for an ASCII fixed-length field file is
  25. relatively straightforward, the offsets of various fields in the ASCII
  26. file being consistent across all rows in the file. However, for comma-
  27. delimited files, this process is slightly more complicated due to the
  28. fact that not all data values in such a file may be the same length for
  29. all rows in the file. This article, then, concentrates on this more
  30. difficult task of reading data from comma-delimited, or varying-length
  31. field, files.
  32.  
  33. The Schema File
  34. ===============
  35.  
  36. The schema file for an ASCII data file contains information that defines
  37. both the file type (comma-delimited versus fixed-length field), as well as
  38. defining the fields that are represented by the data values in each row of
  39. the ASCII data file. (All of the settings used in a schema file are case
  40. insensitive, so "ascii" is just as valid as "ASCII".) In order that a
  41. schema file be recognized as such, it must have the same filename as the
  42. ASCII data file for which it provides definitions, but with the filename
  43. extension .SCH (for SCHema). The attributes that describe the file are:
  44.  
  45.   File name: Enclosed in square brackets, this setting specifies the
  46.              name of the ASCII data file (sans the filename extension,
  47.              which must be .TXT).
  48.  
  49.   Filetype:  Specifies whether the ASCII data file is structured as a
  50.              fixed-length field file (use a setting of FIXED) or a comma-
  51.              delimited file (with data values of potentially varying
  52.              length (use a setting of VARYING).
  53.  
  54.   Delimiter: Specifies the character that surrounds String type data val-
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2844
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  August 17, 1995                          PAGE  :  2/5
  72.  
  73.     TITLE  :  Using The ASCII Driver With Comma-delimited Files
  74.  
  75.  
  76.  
  77.  
  78.              ues (typically, the double quotation mark, ASCII decimal 34).
  79.  
  80.   Separator: Specifies the character that is used to separate individual
  81.              data values (typically, a comma). This character must be a
  82.              visible character, i.e., cannot be a space (ASCII decimal
  83.              32).
  84.  
  85.   CharSet:   Specifies the language driver (use a setting of ASCII).
  86.  
  87. Following the file definition settings are field definitions, one for each
  88. data value on each row of the ASCII data file. These field definitions
  89. supply the information Delphi and the BDE will need to create a virtual
  90. field in memory to hold the data value, that virtual field's data type
  91. which will affect how the value is translated after being read from the
  92. ASCII file, and size and positioning attributes. The various settings that
  93. will appear in each field definition are:
  94.  
  95.   Field:              Virtual field name, will always be "Field" followed
  96.                       by an integer number representing that field's ord-
  97.                       inal position in respect to the other fields in the
  98.                       ASCII data file. E.G., the first field is Field1,
  99.                       the second Field2, and so on.
  100.  
  101.   Field name:         Specifies the display name for the field, which
  102.                       appears as the column header in a TDBGrid. Naming
  103.                       convention for ASCII table fields follows that for
  104.                       Paradox tables.
  105.  
  106.   Field type:         Specifies the data tyoe BDE is to use in translating
  107.                       the data value for each field and tells Delphi what
  108.                       type of virtual field to create.
  109.                       
  110.                       Use the setting For values of type
  111.                       --------------- ---------------------
  112.                       CHAR            Character
  113.                       FLOAT           64-bit floating point
  114.                       NUMBER          16-bit integer
  115.                       BOOL            Boolean (T or F)
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2844
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  August 17, 1995                          PAGE  :  3/5
  133.  
  134.     TITLE  :  Using The ASCII Driver With Comma-delimited Files
  135.  
  136.  
  137.  
  138.  
  139.                       LONGINT         32-bit long integer
  140.                       DATE            Date field.
  141.                       TIME            Time field.
  142.                       TIMESTAMP       Date + Time field.
  143.                       
  144.                       (The actual format for date and time data values
  145.                       will be determined by the current setting in the BDE
  146.                       configuration, Date tab page.)
  147.  
  148.   Data value length:  Maximum length of a field's corresponding data
  149.                       value. This setting determines the length of the
  150.                       virtual field that Delphi creates to receive values
  151.                       read from the ASCII file.
  152.  
  153.   Number of decimals: Applicable to FLOAT type fields; specifies the
  154.                       number of digit positions to the right of the deci-
  155.                       mal place to include in the virtual field defini-
  156.                       tion.
  157.  
  158.   Offset:             Offset from the left that represents the starting
  159.                       position for the field in relation to all of the
  160.                       fields that preceed it.
  161.  
  162. For example, the field definition below is for the first field in the
  163. ASCII table. It defines a String type data value with a name of "Text",
  164. a maximum data value length of three characters (and the field will
  165. appear as only three characters long in Delphi data-aware components such
  166. as the TDBGrid), no decimal places (a String data value will never have
  167. any decimal places), and an offset of zero (because it is the first field
  168. and there would not be any preceeding fields).
  169.  
  170.   Field1=Text,Char,3,00,00
  171.  
  172. Here is an example of a schema file with three fields, the first of String
  173. type and the second and third of type date. This schema file would be
  174. contained in a file named DATES.SCH to provide file and field definitions
  175. for an ASCII data file named DATES.TXT.
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.   PRODUCT  :  Delphi                                 NUMBER  :  2844
  191.   VERSION  :  All
  192.        OS  :  Windows
  193.      DATE  :  August 17, 1995                          PAGE  :  4/5
  194.  
  195.     TITLE  :  Using The ASCII Driver With Comma-delimited Files
  196.  
  197.  
  198.  
  199.  
  200.   [DATES]
  201.   Filetype=VARYING
  202.   Delimiter="
  203.   Separator=,
  204.   CharSet=ascii
  205.   Field1=Text,Char,3,00,00
  206.   Field2=First Contact,Date,10,00,03
  207.   Field3=Second,Date,10,00,13
  208.  
  209. This schema defines a comma-delimited field where all String type data
  210. values can be recognized as being surrounded by the double quotation mark
  211. and where distinct data values are separated by commas (excepting any
  212. commas that may appear within the specified delimiter, inside individual
  213. String data values). The character field has a length of three characters,
  214. no decimal places, and an offset of zero. The first date field has a
  215. length of 10, no decimals, and an offset of three. And the second date
  216. field has a length of 10, no decimals, and an offset of 13.
  217.  
  218. For reading ASCII comma-delimited files, the length and offset parameters
  219. for the field definitions do not apply to data values in the ASCII files
  220. (as is the case for fixed-length field files), but to the virtual fields,
  221. defined in the application, into which the values read will be placed. The
  222. length parameter will need to reflect the maximum length of the data value
  223. for each field -- not counting the delimiting quotation marks or the comma
  224. separators. This is most difficult to estimate for String type data values
  225. as the actual length of such a data value may vary greatly from row to row
  226. in the ASCII data file. The offset parameter for each field will not be
  227. the position of the data value in the ASCII file (as is the case for
  228. fixed-length field files), but the offset as represented by the cumulative
  229. length of all preceding fields (again, the defined fields in memory, not
  230. the data values in the ASCII file.
  231.  
  232. Here is a data file that would correspond to the schema file described
  233. above, in a file named DATES.TXT:
  234.  
  235.   "A",08/01/1995,08/11/1995 
  236.   "BB",08/02/1995,08/12/1995 
  237.   "CCC",08/03/1995,08/13/1995
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.   PRODUCT  :  Delphi                                 NUMBER  :  2844
  252.   VERSION  :  All
  253.        OS  :  Windows
  254.      DATE  :  August 17, 1995                          PAGE  :  5/5
  255.  
  256.     TITLE  :  Using The ASCII Driver With Comma-delimited Files
  257.  
  258.  
  259.  
  260.  
  261.  
  262. The maximum length of an actual data value in the first field is three
  263. ("CCC"). because this is the first field and there are no preceding
  264. fields, the offset for this field is zero. The length of this first field
  265. (3) is used as the offset for the second field. The length of the second
  266. field, a date value, is 10, reflecting the maximum length of a data value
  267. for that field. The accumulated length of the first and second fields are
  268. then used as the offset for the third field (3 + 10 = 13) .
  269.  
  270. It is only when the proper length for the data values in the ASCII file
  271. are used and each fields length added to any preceding fields to produce
  272. offset values for succeeding fields that this process will correctly read
  273. the data. If data is misread because of improper length settings in the
  274. schema file, most values will suffer adverse translation effects, such
  275. as truncation of character data or numeric values being interpreted as
  276. zeros. Data will usually still be displayed, but no error should occur.
  277. However, values that must be in a specific format in order to be trans-
  278. lated into the appropriate data type will cause errors if the value read
  279. includes characters not valid in a date value. This would include a date
  280. data value which, when incorrectly read may contain extraneous characters
  281. from other surrounding fields. Such a condition will result in a data
  282. translation exception requiring an adjustment of the field length and
  283. offset settings in the schema file.
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. DISCLAIMER: You have the right to use this technical information
  302. subject to the terms of the No-Nonsense License Statement that
  303. you received with the Borland product to which this information
  304. pertains.
  305.